/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.huhu.contact.view;

import com.huhu.contact.model.ContactBean;
import com.huhu.contact.provider.StickyContactProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

import java.util.List;

/**
 * 滑动逻辑处理
 *
 * @since 2021-04-21
 */
public class HeaderDecor {
    private static final int UPSCROLL = 1;
    private static final int DOWNSCROLL = 2;
    private static final int FIRST_ITEM = 1;
    private static final int SECOND_ITEM = 2;
    private static final int UNIT_Y = 20;
    private static final int MARGIN_TOP = 48;
    private ListContainer listContainer;
    private Text headerText;
    private float headerTextY = 0;
    private int scrollType = 0;
    /**
     * 下滑时是否要更改上方悬停标题
     */
    private boolean isChangeTitle = false;
    /**
     * onTouchEvent事件中记录down时的Y轴值
     */
    private float mDownY = 0;
    /**
     * 单头部显示的字符串
     */
    private String headerString = "Header 0";

    /**
     * 单header构造函数
     *
     * @param listContainer 滑动控件
     * @param oneText       头部Header
     */
    public HeaderDecor(ListContainer listContainer, Text oneText) {
        this.listContainer = listContainer;
        this.headerText = oneText;
        this.headerTextY = headerText.getHeight();
        setTouchEventLisener();
        setScrolledListener();
    }

    public List<ContactBean> getDataList() {
        return ((StickyContactProvider)this.listContainer.getItemProvider()).getData();
    }

    private void setTouchEventLisener() {
        listContainer.setTouchEventListener((component, touchEvent) -> {
            int action = touchEvent.getAction();
            MmiPoint point = touchEvent.getPointerPosition(touchEvent.getIndex());
            switch (action) {
                case TouchEvent.PRIMARY_POINT_DOWN:
                    return true;
                case TouchEvent.POINT_MOVE:
                    if (point.getY() > mDownY) {
                        scrollType = DOWNSCROLL;
                    } else if (point.getY() < mDownY) {
                        scrollType = UPSCROLL;
                    }
                    mDownY = point.getY();
                    break;
                case TouchEvent.PRIMARY_POINT_UP:
                    mDownY = 0;
                    break;
                case TouchEvent.NONE:
                case TouchEvent.CANCEL:
                    mDownY = point.getY();
                    return true;
                default:
                    break;
            }
            return false;
        });
    }

    private void setScrolledListener() {
        listContainer.setScrolledListener((component, scrollX, scrollY, oldScrollX, oldScrollY) -> {

            if (scrollType == DOWNSCROLL) {
                downMove();
            } else if (scrollType == UPSCROLL) {
                upMove();
            }
        });
    }

    /**
     * 向下滑动
     */

    private void downMove() {
        List<ContactBean> dataList = getDataList();
        int firstIndex = listContainer.getItemPosByVisibleIndex(0);
        if (firstIndex < 0 || firstIndex >= dataList.size()) {
            return;
        }
        int index = listContainer.getItemPosByVisibleIndex(1);
        boolean isHeader = dataList.get(index).isHeader();
        Component firstView = listContainer.getComponentAt(index);
        if (firstView == null) {
            return;
        }
        int firstViewTop = firstView.getTop();
        if (firstIndex == 0) {
            if (!dataList.get(0).isHeader()) {
                headerText.setVisibility(Component.HIDE);
            } else {
                if (firstViewTop >= headerTextY) {
                    headerText.setVisibility(Component.HIDE);
                }
            }
            return;
        }
        if (isHeader) {
            isChangeTitle = true;
            float moveY = firstViewTop - headerTextY;
            if (moveY <= 0) {
                headerText.setContentPositionY(moveY);
            }
            for (int i = index - 1; i >= 0; i--) {
                if (dataList.get(i).isHeader()) {
                    headerString = dataList.get(i).getCharacter();
                    break;
                }
            }
            headerText.setText(headerString);
        } else {
            if (isChangeTitle && index != 0) {
                headerText.setText(headerString);
            }
            headerText.setContentPositionY(0);
            isChangeTitle = false;
        }
    }

    /**
     * 向上滑动
     */
    private void upMove() {
        List<ContactBean> dataList = getDataList();
        // 获取当前屏幕完整显示的第1个item的下标
        int index = listContainer.getItemPosByVisibleIndex(1);
        if (index < 0 || index >= dataList.size()) {
            return;
        }
        boolean isHeader = dataList.get(index).isHeader();
        Component firstView = listContainer.getComponentAt(index);
        if (firstView == null) {
            return;
        }
        int firstViewTop = firstView.getTop();
        if (index == 1 && firstViewTop <= headerTextY) {
            if (dataList.get(0).isHeader()) {
                headerText.setText(dataList.get(0).getCharacter());
                headerText.setVisibility(Component.VISIBLE);
            }
        }

        if (isHeader) {
            isChangeTitle = true;
            float moveY = firstViewTop - headerTextY;
            if (moveY <= 0) {
                headerText.setContentPositionY(moveY);
            }
            ContactBean itemModel= dataList.get(index);
            if (itemModel != null) {
                headerString = itemModel.getCharacter();
            }
        } else {
            if (isChangeTitle && index != 0) {
                headerText.setText(headerString);
                headerText.setVisibility(Component.VISIBLE);
            }
            headerText.setContentPositionY(0);
            isChangeTitle = false;
        }
    }
}
